﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Library.Common;
using System.Security.Cryptography;
using Library.Bll;

namespace HanhuaMicroCredit.Admin.Controllers
{
    public class SysUsersController : UserAuthenController
    {
        //
        // GET: /SysUsers/
        #region //修改密码
        public ActionResult password()
        {
            return PartialView("~/views/sysusers/password/password.ascx");
            //return View("~/views/sysusers/password/password.aspx");
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        [HttpPost]
        public ContentResult changePW(string rturl,FormCollection fc)
        {
            try
            {
                Dictionary<string,object> dic_pw=new Dictionary<string,object>();
                foreach (string s in fc.Keys)
                {
                    dic_pw.Add(s, fc[s]);
                }
                string[] new_pw = { "_1","_2","_3","_4"};
                if (dic_pw.ContainsKey("new_PW"))
                {
                    new_pw=dic_pw["new_PW"].ToString().Split(',');
                    if (new_pw[0] != new_pw[1])
                    {
                        return Content(JUIResult.ResultMsg("新密码不一致"));
                    }
                }
                if (dic_pw.ContainsKey("old_PW"))
                {
                    string old_pw = GetMD5(dic_pw["old_PW"].ToString());
                    string sql_pw = SelectTableBLL.getNameById("id", Current.GetUserInfo("userid"), "Sys_IBUsers", "PassWord");
                    if (old_pw.ToLower() == sql_pw.ToLower())
                    {
                        if (new_pw[0] != "_1")
                        { 
                            Dictionary<string,object> dic_newpw=new Dictionary<string,object>();
                            dic_newpw.Add("PassWord",GetMD5(new_pw[0]));

                            CommonBLL.Update("Sys_IBUsers", dic_newpw, "id=" + Current.GetUserInfo("userid"));
                            return Content(JUIResult.Result(4, "200", "", rturl, ""));
                        }
                    }
                }
                    return Content(JUIResult.ResultMsg("原密码错误"));
                //return Content(JUIResult.ResultUrl(rturl));
            }
            catch (Exception ex)
            {
                return Content(JUIResult.ResultMsg(ex.Message));
            }
        }
        #endregion
        #region //重置密码
        public ActionResult rePW(string id)
        {
            return PartialView("~/views/sysusers/PassWord/retPW.ascx", id);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        [HttpPost]
        public ContentResult retPW(string dtnum, string ids, string v, string url)
        {
            try
            {
                string rturl = HttpUtility.UrlDecode(url);

                int dt = Utils.StrToInt(dtnum, 1) - 1;
                Database database = (Database)dt;
                ids = Utils.ChkSQL(ids);
                //
                string condition = string.Format("id in ({0})", ids);
                //
                Dictionary<string, object> d = new Dictionary<string, object>();
                d.Add("PassWord", GetMD5(v));
                //启用
                CommonBLL.Update(database, "Sys_IBUsers", d, condition);

                return Content(JUIResult.ResultUrl(rturl));
            }
            catch (Exception ex)
            {
                return Content(JUIResult.ResultMsg(ex.Message));
            }
        }
        #endregion



        #region //修改个人信息
        public ActionResult Editinfo()
        {
            return PartialView("~/views/sysusers/Editinfo/info.ascx");
        }
        [HttpPost]
        public ContentResult Edit_info(string dtnum, string ids, FormCollection fc, string url)
        {
            try
            {
                string rturl = HttpUtility.UrlDecode(url);

                int dt = Utils.StrToInt(dtnum, 1) - 1;
                Database database = (Database)dt;
                ids = Utils.ChkSQL(ids);
                //
                string condition = string.Format("id in ({0})", ids);
                //
                Dictionary<string, object> d = new Dictionary<string, object>();
                foreach (string s in fc.Keys)
                {
                    if (s.IndexOf("u0_") > -1)
                    {
                        d.Add(s.Replace("u0_", ""), fc[s]);
                    }
                }
                //启用
                CommonBLL.Update(database, "Sys_IBUsers", d, condition);

                return Content(JUIResult.ResultUrl(rturl));
            }
            catch (Exception ex)
            {
                return Content(JUIResult.ResultMsg(ex.Message));
            }
        }

        #endregion
        /// <summary>
        /// 产生md5值
        /// </summary>
        /// <param name="sDataIn"></param>
        /// <returns></returns>
        private static string GetMD5(string sDataIn)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bytValue, bytHash;
            bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
            bytHash = md5.ComputeHash(bytValue);
            md5.Clear();
            string sTemp = "";
            for (int i = 0; i < bytHash.Length; i++)
            {
                sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
            }
            return sTemp.ToLower();
        }
    }
}
